package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.sgb.member.R;
import com.sgb.member.databinding.DialogMemberMoreBinding;
import com.sgb.member.entity.MemberBean;

import java.util.Objects;

public class MemberMoreDialog extends DialogFragment implements View.OnClickListener {

    private DialogMemberMoreBinding mBinding;
    private final MemberBean mMemberBean;
    private final int mPos;
    private final boolean hideDissolution; //隐藏解除会员
    private MemberCallback memberCallback;

    public void setMemberCallback(MemberCallback memberCallback) {
        this.memberCallback = memberCallback;
    }

    public MemberMoreDialog(MemberCallback memberCallback, int pos, MemberBean bean) {
        this(memberCallback, pos, bean, false);
    }


    public MemberMoreDialog(MemberCallback memberCallback, int pos, MemberBean bean, boolean hideDissolution) {
        setMemberCallback(memberCallback);
        mPos = pos;
        mMemberBean = bean;
        this.hideDissolution = hideDissolution;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MemberDialogTheme);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = Objects.requireNonNull(this.getDialog()).getWindow();
        //去掉dialog默认的padding
        assert window != null;
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_member_more, container, false);
        mBinding.setLifecycleOwner(this);
        mBinding.tvCompany.setText(mMemberBean.getCompanyName());
        mBinding.tvAction1.setOnClickListener(this);
        mBinding.tvAction2.setOnClickListener(this);
        mBinding.tvAction3.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
        mBinding.tvAction2.setVisibility(hideDissolution ? View.GONE : View.VISIBLE);
        return mBinding.getRoot();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_action1) {
            dismiss();
            if (memberCallback != null) {
                memberCallback.onMemberTransfer(mPos, mMemberBean);
            }
        } else if (id == R.id.tv_action2) {
            dismiss();
            if (memberCallback != null) {
                memberCallback.onMemberDissolution(mPos, mMemberBean);
            }
        } else if (id == R.id.tv_action3) {
            dismiss();
            if (memberCallback != null) {
                memberCallback.onMemberModify(mPos, mMemberBean);
            }
        } else if (id == R.id.tv_cancel) {
            dismiss();
        }
    }

    /**
     * 显示Dialog
     *
     * @param activity
     * @param tag      设置一个标签用来标记Dialog
     */
    public void show(AppCompatActivity activity, String tag) {
        show(activity, null, tag);
    }

    public void show(Context context, String tag) {
        show((AppCompatActivity) context, null, tag);
    }

    /**
     * 显示Dialog
     *
     * @param activity
     * @param bundle   要传递给Dialog的Bundle对象
     * @param tag      设置一个标签用来标记Dialog
     */
    public void show(AppCompatActivity activity, Bundle bundle, String tag) {
        if (activity == null && isShowing()) {
            return;
        }
        assert activity != null;
        FragmentTransaction mTransaction = activity.getSupportFragmentManager().beginTransaction();
        Fragment mFragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (mFragment != null) {
            //为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            mTransaction.remove(mFragment);
        }
        if (bundle != null) {
            setArguments(bundle);
        }
        show(mTransaction, tag);
    }

    /**
     * 是否显示
     *
     * @return false:isHidden  true:isShowing
     */
    protected boolean isShowing() {
        if (this.getDialog() != null) {
            return this.getDialog().isShowing();
        } else {
            return false;
        }
    }

}
